package servlet;

import bean.Grade;
import bean.Student;
import service.StudentService;
import service.impl.GradeServiceImpl;
import service.impl.StudentServiceImpl;
import utils.PageUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = "/Educational/student/Student")
public class StudentServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if ("addstu".equals(method)) {
            addstu(req, resp);
        }else if ("findbyid".equals(method)){
            findbyid(req, resp);
        }else if ("findbystuno".equals(method)){
            findbystuno(req, resp);
        }else if ("update".equals(method)){
            update(req, resp);
        }else if ("delete".equals(method)){
            delete(req, resp);
        }else {
            getStudentList(req, resp);
        }
    }
        protected void getStudentList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


            PageUtils pu = new PageUtils();
            String stuname = req.getParameter("stuname");
            String stuno = req.getParameter("stuno");
            String sex = req.getParameter("sex");
            req.setAttribute("stuname",stuname);
            req.setAttribute("stuno",stuno);
            req.setAttribute("sex",sex);
            String pageindex = req.getParameter("pageindex");
            int index=pageindex==null||pageindex.equals("")?1:Integer.parseInt(pageindex);
            StudentService studentService = new StudentServiceImpl();
            List<Student> students = studentService.getStudents(stuname, stuno, sex==null||sex.equals("")?-1:Integer.parseInt(sex),(index-1)*pu.getPagesize(),pu.getPagesize());
            int total = studentService.getTotal(stuname,stuno,sex==null||sex.equals("")?-1:Integer.parseInt(sex));
            if (students!=null){
                pu.setDataList(students);
                pu.setPageindex(index);
                pu.setTotal(total);
                req.setAttribute("p1",pu);
                req.getRequestDispatcher("list.jsp").forward(req,resp);
            }

        }
    protected void addstu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String stuname = req.getParameter("stuname");
        String stuno = req.getParameter("stuno");
        String sex = req.getParameter("sex");
        String gid = req.getParameter("gid");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String profession = req.getParameter("profession");
        String address = req.getParameter("address");
        String idnumber = req.getParameter("idnumber");
        String registered = req.getParameter("registered");
        String introduction = req.getParameter("introduction");
        String politics = req.getParameter("politics");
        Student student = new Student();

        student.setStuname(stuname);
        student.setStuno(stuno);
        student.setSex(Integer.parseInt(sex));
        student.setPhone(phone);
        student.setEmail(email);
        student.setRegistered(registered);
        student.setAddress(address);
        student.setProfession(profession);
        student.setIdnumber(idnumber);
        student.setPolitics(politics);
        student.setIntroduction(introduction);
        student.setGid(Integer.parseInt(gid));
        StudentServiceImpl ss = new StudentServiceImpl();
        int k = ss.addstu(student);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (k>0){
            writer.println("<script>location.href='Student';alert('增加成功')</script>");
        }else{
            writer.println("<script>location.href='Grade';alert('增加失败')</script>");
        }
    }


        protected void findbyid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String stuid = req.getParameter("stuid");
            StudentServiceImpl ss = new StudentServiceImpl();
            Student student = ss.findById(Integer.parseInt(stuid));
            GradeServiceImpl gs = new GradeServiceImpl();
            List<Grade> grades = gs.getGrades();
            req.setAttribute("grades",grades);
            req.setAttribute("student",student);
            req.getRequestDispatcher("edit.jsp").forward(req,resp);
        }
        protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String stuid = req.getParameter("stuid");
            String stuname = req.getParameter("stuname");
            String stuno = req.getParameter("stuno");
            String sex = req.getParameter("sex");
            String gid = req.getParameter("gid");
            String email = req.getParameter("email");
            String phone = req.getParameter("phone");
            String profession = req.getParameter("profession");
            String address = req.getParameter("address");
            String idnumber = req.getParameter("idnumber");
            String registered = req.getParameter("registered");
            String introduction = req.getParameter("introduction");
            String politics = req.getParameter("politics");
            Student student = new Student();
            student.setStuid(Integer.parseInt(stuid));
            student.setStuname(stuname);
            student.setStuno(stuno);
            student.setSex(sex==null||sex.equals("")?-1:Integer.parseInt(sex));
            student.setPhone(phone);
            student.setEmail(email);
            student.setRegistered(registered);
            student.setAddress(address);
            student.setProfession(profession);
            student.setIdnumber(idnumber);
            student.setPolitics(politics);
            student.setIntroduction(introduction);
            student.setGid(Integer.parseInt(gid));
            StudentServiceImpl ss = new StudentServiceImpl();
            int k = ss.updateStudent(student);
            resp.setContentType("text/html;charset=utf-8");
            PrintWriter writer = resp.getWriter();
            if (k>0){
                writer.println("<script>location.href='Student';alert('修改成功')</script>");
            }else{
                writer.println("<script>location.href='findbyid';alert('修改失败')</script>");
            }
        }
        protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String stuid1 = req.getParameter("stuid");
            int stuid = stuid1==null||stuid1.equals("")?-1:Integer.parseInt(stuid1);
            StudentServiceImpl ss = new StudentServiceImpl();
            int k = ss.deleteStudent(stuid);
            resp.setContentType("text/html;charset=utf-8");
            PrintWriter writer = resp.getWriter();
            if (k>0){
                writer.println("<script>location.href='Student';alert('删除成功')</script>");
            }else{
                writer.println("<script>location.href='list.jsp';alert('删除失败')</script>");
            }
        }
    protected void findbystuno(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String studentnumber = req.getParameter("studentnumber");
        int stunumber = studentnumber==null||studentnumber.equals("")?1102:Integer.parseInt(studentnumber);
        StudentService studentService = new StudentServiceImpl();
        int i = studentService.findByStuno(stunumber);
        System.out.println("shenm"+i);
        if (i==1){
            resp.setContentType("text/html;charset=utf-8");
            PrintWriter writer = resp.getWriter();
            writer.println("用户名重复或无效");
            writer.close();
        }
    }
}
